--
-- Author: yawei.zhu
-- Date: 2018-09-18 14:38:53
--
-- DynastyRightView
--

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.DynastyRightScene

local BATTLE_COUNT = 3

function ClassRef:init()
	self._ui.tlt_2:setString(L("dynastybattle_score1"))

	self._ui.btn_l:addClickEventListener(function()
		self:showIndex(self._curIndex - 1)
	end)
	self._ui.btn_r:addClickEventListener(function()
		self:showIndex(self._curIndex + 1)
	end)
	self._ui.Button:addClickEventListener(function()
		executeFunc(self._onOperate, "changeStronghold", {self._townId, self._strongholds[self._curIndex].id})
	end)
end

function ClassRef:showIndex(index)
	if index < 1 or index > self._maxCount then return end 
	self._curIndex = index

	local stronghold = self._strongholds[index]
	if self._myIsAtk then
		self._ui.num_1:setString( stronghold.realNumAtk )
		self._ui.num_2:setString( stronghold.realNumDef )
		self._ui.num_3:setString( stronghold.realSpeedAtk )
		self._ui.num_4:setString( stronghold.realSpeedDef )
	else
		self._ui.num_1:setString( stronghold.realNumDef )
		self._ui.num_2:setString( stronghold.realNumAtk )
		self._ui.num_3:setString( stronghold.realSpeedDef )
		self._ui.num_4:setString( stronghold.realSpeedAtk )
	end

	display.setBtnTouchEnable(self._ui.btn_l, true)
	display.setBtnTouchEnable(self._ui.btn_r, true)
	if index <= 1 then 
		display.setBtnTouchEnable(self._ui.btn_l, false)
	end
	if index >= self._maxCount then
		display.setBtnTouchEnable(self._ui.btn_r, false)
	end

	local strongholdConfig = GD:getStrongholdInfoById(stronghold.id)
	self._ui.name:setString(L(strongholdConfig.name))
end

-- townInfo 战场信息
-- index 当前据点的索引
-- myGuildID 我的公会ID
function ClassRef:updateView(townInfo, index, myGuildID)
	if not townInfo then return end
	if not index then index = 1 end

	self._townId = townInfo.id
	self._strongholds = townInfo.strongholds 
	self._curIndex = self._curIndex or index
	self._myIsAtk = (myGuildID == townInfo.atker.guildId)
	self._maxCount = BATTLE_COUNT

	self:showIndex(self._curIndex)
end

return ClassRef

